<template>
  <div class="barcode">
    <div>
      <button @click="camera">打开相机选择照片</button>
      <br>
      {{type}} <br> {{result}}
    </div>
    <br><br><br>
    <div>
      <button @click="saoma">直接进行扫码</button>
    </div>
    <div id="bcid" v-show="bcidShow"></div>
  </div>
</template>

<script>
  var barcode = null;
  export default {
    name: "barcode",
    data() {
      return {
        type: "",
        result: "",
        bcidShow:false
      }
    },
    methods: {
      camera() {
        const that = this;
        if (mui.os.plus) {
          var buttonTit = [{
            title: "拍照"
          }, {
            title: "从手机相册选择"
          }];
          plus.nativeUI.actionSheet({
            // title: "上传图片",
            cancel: "取消",
            buttons: buttonTit
          }, function (res) { /*actionSheet 按钮点击事件*/
            switch (res.index) {
              case 0:
                break;
              case 1:
                that.getImage();
                /*拍照*/
                break;
              case 2:
                that.galleryImg();
                /*打开相册*/
                break;
              default:
                break;
            }
          })
        }
      },
      getImage() {
        const that = this;
        var c = plus.camera.getCamera();
        c.captureImage(function (e) {
          plus.io.resolveLocalFileSystemURL(e, function (entry) {
            var imgSrc = entry.toLocalURL() + "?version=" + new Date().getTime(); //拿到图片路径
            console.log(imgSrc);
            that.scanImg(that, imgSrc);
          }, function (e) {
            console.log("读取拍照文件错误：" + e.message);
          });
        }, function (s) {
          console.log("error" + s.message);
        }, {
          filename: "_doc/camera/"
        })
      },
      galleryImg() {
        const that = this;
        plus.gallery.pick(function (e) {
          for (var i in e.files) {
            var fileSrc = e.files[i];
            console.log(fileSrc);
            that.scanImg(that, fileSrc);
          }
        }, function (e) {
          console.log("取消选择图片");
        }, {
          filter: "image",
          multiple: true,
          maximum: 1,
          system: false
        });
      },
      scanImg(that, urlImg) {
        plus.barcode.scan(urlImg, function (type, result) {
          console.log(JSON.stringify(type + result));
          that.type = type;
          that.result = result;
        }, function (e) {
          mui.alert("Scan failed: " + JSON.stringify(e), "", "", "", "div");
        });
      },
      saoma() {
        this.bcidShow=true;
        barcode = new plus.barcode.Barcode('bcid');
        barcode.onmarked = function (type, result) {
          console.log('Success: type=' + type + ', result=' + result);
        };
        barcode.start();
      }
    }
  }
</script>

<style scoped lang="scss">
  .barcode {
    width: 100%;
    height: 100%;

    #bcid {
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.3);
      position: absolute;
      top: 0;
      left: 0;
    }
  }
</style>
